<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>styleFrame</title>
</head>

<body>
  <div class="frame" style="display:none"></div>
  <textarea type="text" id="domFrame" cols="30" rows="10" placeholder="Paste HTML"></textarea>
  <textarea id="styleFrame" cols="30" rows="10" disabled placeholder="Display styleFrame"></textarea>
  <button id="reload" style="display:none">clean</button>
  <div class="desc">
    <h1>DomTree to StyleFrame</h1>
    <p>this tool will help you tranform style frame from dom tree which can be used in sass/styus syntax</p>
    <a href="https://marketplace.visualstudio.com/items?itemName=browniu.styleframe">vscode Plugin</a><span>|</span>from
    <a href="https://browniu.com">browniu</a>
  </div>
</body>
<style>
  textarea {
    outline: none;
    margin: 5px;
  }

  .desc {
    margin: 10px;
  }

  h1 {
    font-size: 32px;
  }

  span {
    margin: 10px;
    opacity: .3;
  }
</style>
<script>
  class DomTree {
    constructor(dom) {
      this.domRoot = dom
      this.tree = {}
      this.init()
    }
    init() {
      this.renderDom()
    }
    traver(dom, branch) {
      for (let i = 0; i < this.picker(dom).length; i++) {
        const element = this.picker(dom)[i];
        if (element.nodeType === 1) {
          let branchName = element.tagName.toLowerCase()
          if (element.className) {
            branchName = '.' + element.className
          }
          if (element.childNodes.length > 1 || (element.childNodes.length === 1 && element.childNodes[0].nodeType ===
              1)) {
            if (typeof branch[branchName] != 'object') branch[branchName] = {}
            this.traver(element, branch[branchName])
          } else {
            if (!branch[branchName]) branch[branchName] = '{}'
          }
        }
      }
    }
    picker(dom) {
      let doms = []
      for (let i = 0; i < dom.childNodes.length; i++) {
        const element = dom.childNodes[i];
        if (element.nodeType === 1 && !element.nodeName.match('STYLE') && !element.nodeName.match('SCRIPT')) {
          doms.push(element)
        }
      }
      return doms
    }
    transform(root, obj) {
      let result = '_' + JSON.stringify(obj).replace(/\"/g, '').replace(/\,/g, ';') + '_'
      result = result.split('_{')[1].split('}_')[0]
      return result
    }
    renderDom() {
      document.querySelector('#domFrame').oninput = () => {
        document.querySelector('.frame').innerHTML = document.querySelector('#domFrame').value
        document.querySelector('#domFrame').style.display = 'none'
        document.querySelector('#reload').style.display = 'block'
        document.querySelector('#reload').addEventListener('click', () => {
          window.location.reload()
        })
        this.picker(this.domRoot)
        this.traver(this.domRoot, this.tree)
        document.querySelector('#styleFrame').innerHTML = this.transform('body', this.tree)
      }
    }
  }
  let domTree = new DomTree(document.querySelector('.frame'))
</script>

</html>